<template>
  <div class="backColor">
    <div class="swiper">
      <van-swipe :autoplay="3000" indicator-color="#CAA674">
        <van-swipe-item
          v-for="(item, index) in info.data.listAlbum"
          :key="index"
          @click="seeImage(index)"
        >
          <img :src="item" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="content">
      <div class="title">{{ info.data.name }}</div>
      <div class="number">商品编号：{{ info.data.number }}</div>
      <div class="money">
        <span>{{
          info.data.productStatus == "2" ? "买入价格" : "售出价格"
        }}</span>
        <div>
          ￥<span>{{ info.data.price }}</span>
        </div>
      </div>
      <div class="quantity">
        库存数量：{{ info.data.productStatus == "0" || "2" || "3" ? "1" : "0" }}
      </div>
    </div>
    <div class="details">
      <div class="text">商品详情</div>
      <div class="html" v-html="info.data.detail"></div>
    </div>
    <div class="buttons">
      <div class="left" @click="router.go(-1)">返回列表</div>
      <div class="right" @click="submit" v-if="info.type != 4">
        {{ info.buttonTxt }}
      </div>
    </div>
  </div>
</template>

<script>
import {
  defineComponent,
  onMounted,
  provide,
  reactive,
  ref,
  getCurrentInstance,
} from "vue";
import { useRouter, useRoute } from "vue-router";
import { ImagePreview } from "vant";
import { getUserDetail, imgUrl } from "@/api/http.js";

export default defineComponent({
  setup() {
    const { proxy } = getCurrentInstance();
    const router = useRouter();
    const route = useRoute();
    const info = reactive({
      data: {},
      buttonTxt: "",
    });
    const seeImage = (index) => {
      ImagePreview({
        images: info.data.listAlbum,
        startPosition: index,
      });
    };

    const overlayBool = ref(false);
    provide("overlay", overlayBool);
    const clickOverlay = () => {
      overlayBool.value = true;
    };

    const submit = () => {
      if (info.data.productStatus == "1")
        return proxy.$toast.fail("商品已售出");
      router.push({
        name: "MyGoodsTrade",
        query: {
          orderId: route.query.orderId,
        },
      });
    };

    const getInfo = async () => {
      const data = await getUserDetail({ id: route.query.orderId });
      data.data.listAlbum = data.data.listAlbum
        .split(",")
        .map((item) => imgUrl + item);
      info.data = data.data;
      getButtonTet();
      console.log("data---", data);
    };

    const getButtonTet = () => {
      let txt;
      switch (info.data.productStatus) {
        case "0":
          txt = "上架中";
          break;

        case "1":
          txt = "已售出";
          break;

        case "2":
          txt = "暂未上架";
          break;

        case "3":
          txt = "审核中";
          break;

        case "4":
          txt = "等待收款";
          break;

        default:
          break;
      }
      info.buttonTxt = txt;
    };

    onMounted(() => {
      getInfo();
    });

    return {
      seeImage,
      info,
      router,
      clickOverlay,
      overlayBool,
      submit,
    };
  },
});
</script>

<style lang="less" scoped>
.backColor {
  min-height: 100vh;
  background-color: var(--backColor);
  .swiper {
    width: 100%;
    height: 300px;
    overflow: hidden;

    img {
      height: 300px;
    }
  }
  .content {
    width: 100%;
    padding: 20px 4%;
    background-color: var(--backColor);
    color: var(--textColor);

    .title {
      font-size: 1.125rem;
      line-height: 25px;
      letter-spacing: 0.5px;
      font-family: PingFang SC;
      font-weight: bold;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2; /*想省略几行就写几*/
      -webkit-box-orient: vertical;
    }

    .number {
      margin-top: 20px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: var(--textColorTwo);
    }

    .money {
      margin-top: 20px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: var(--textColorTwo);
      display: flex;
      justify-content: space-between;
      align-content: flex-end;

      & > span {
        line-height: 35px;
      }

      div {
        color: #dd0000;

        span {
          font-size: 1.56rem;
        }
      }
    }

    .quantity {
      margin-top: 10px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      padding-bottom: 20px;
      border-bottom: 1px solid var(--textColorTwo);
    }
  }
  .details {
    width: 100%;
    padding: 20px 4% 60px;
    background-color: var(--backColor);
    color: var(--textColor);

    .html {
      width: 100%;
      margin-top: 20px;
      overflow: hidden;
    }
  }
  .buttons {
    width: 100%;
    height: 60px;
    position: fixed;
    left: 0;
    z-index: 99;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    letter-spacing: 2px;

    div {
      width: 100%;
      height: 100%;
      font-size: 1.125rem;
      font-family: PingFang SC;
      font-weight: bold;
      line-height: 60px;
      text-align: center;
    }

    .left {
      background-color: #fff;
      color: #caa674;
    }
    .right {
      background-color: #caa674;
      color: #fff;
    }
  }
}
.dialog {
  img {
    width: 100%;
  }
}
</style>
